local bowen = fk.CreateSkill{
  name = "xiaobai__bowen",
}

Fk:loadTranslationTable{
  ["xiaobai__bowen"] = "博文",
  [":xiaobai__bowen"] = "出牌阶段限X次，你可以选择一项：1.观看牌堆顶X张牌，然后可以弃置任意张牌，获得其中牌名字数之和不大于你弃置牌的牌；"..
  "2.视为使用一张牌名字数为X的基本牌或普通锦囊牌，然后本阶段结束。（X为你本阶段已使用的牌数，至多为5）",
  ["#xiaobai__bowen1"] = "博文：观看牌堆顶%arg张牌，然后可以弃置牌获得其中的牌",
  ["#xiaobai__bowen2"] = "博文：视为使用字数为%arg的牌，然后本阶段结束",
  ["xiaobai__bowen1"] = "观看牌堆",
  ["#xiaobai__bowen-exchange"] = "博文：弃置任意张牌，获得牌名字数之和不大于弃牌的牌",

  ["$xiaobai__bowen1"] = "何必为国之大器？聊以自乐耳。",
  ["$xiaobai__bowen2"] = "唯以博学，而成一世风流。"
}

bowen:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = function (self, player, selected_cards, selected)
    if self.interaction.data == "xiaobai__bowen1" then
      return "#xiaobai__bowen1:::"..player:getMark("xiaobai__bowen-phase")
    else
      return "#xiaobai__bowen2:::"..player:getMark("xiaobai__bowen-phase")
    end
  end,
  times = function(self, player)
    return player.phase == Player.Play and player:getMark("xiaobai__bowen-phase") - player:usedSkillTimes(bowen.name, Player.HistoryPhase) or -1
  end,
  interaction = function(self, player)
    local all_choices = {"xiaobai__bowen1"}
    local all_names = {}
    for _, name in ipairs(Fk:getAllCardNames("bt")) do
      if Fk:translate(Fk:cloneCard(name).trueName, "zh_CN"):len() == player:getMark("xiaobai__bowen-phase") then
        table.insert(all_choices, name)
        table.insert(all_names, name)
      end
    end
    local choices = player:getViewAsCardNames(bowen.name, all_names)
    table.insert(choices, 1, "xiaobai__bowen1")
    return UI.CardNameBox {
      choices = choices,
      all_choices = all_choices,
      default_choice = "xiaobai__bowen1",
    }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(bowen.name, Player.HistoryPhase) < player:getMark("xiaobai__bowen-phase")
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if self.interaction.data == "xiaobai__bowen1" then
      return false
    else
      local card = Fk:cloneCard(self.interaction.data)
      card.skillName = bowen.name
      return card.skill:targetFilter(player, to_select, selected, {}, card, {bypass_times = true})
    end
  end,
  feasible = function (self, player, selected, selected_cards)
    if self.interaction.data == "xiaobai__bowen1" then
      return #selected == 0
    else
      local card = Fk:cloneCard(self.interaction.data)
      card.skillName = bowen.name
      return card.skill:feasible(player, selected, {}, card)
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    if self.interaction.data == "xiaobai__bowen1" then
      local to_discard = table.filter(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id)
      end)
      local cards = room:askToPoxi(player, {
        poxi_type = "xiaobai__bowen", 
        data = {{ "Top", room:getNCards(player:getMark("xiaobai__bowen-phase")) },
          {player.general, to_discard },}, 
        cancelable = true})
      if #cards == 0 then return end
      local cards1 = table.filter(cards, function (id)
        return table.contains(to_discard, id)
      end)
      local cards2 = table.filter(cards, function (id)
        return table.contains(room.draw_pile, id)
      end)
      room:moveCards({
        ids = cards1,
        from = player,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        skillName = bowen.name,
        proposer = player,
        moveVisible = true,
      },
      {
        ids = cards2,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = bowen.name,
        proposer = player,
        moveVisible = true,
      })
    else
      local c = Fk:cloneCard(self.interaction.data)
      c.skillName = bowen.name
      local use = {
        from = player,
        tos = effect.tos,
        card = c,
        extraUse = true,
      }
      use.card.skillName = bowen.name
      room:useCard(use)
      player:endCurrentPhase()
    end
  end,

  on_acquire = function (self, player, is_start)
    local room = player.room ---@type Room
    if player.phase == Player.Play then
      local events = room.logic:getEventsOfScope(GameEvent.UseCard, 5, function (e)
        return e.data.from == player
      end, Player.HistoryPhase)
      room:setPlayerMark(player, "xiaobai__bowen-phase", #events)
    end
  end,
})
Fk:addPoxiMethod{
  name = "xiaobai__bowen",
  prompt = "#xiaobai__bowen-exchange",
  card_filter = function(to_select, selected, data, extra_data)
    if table.contains(data[2][2], to_select) then
      return true
    else
      local n1, n2 = 0, 0
      for _, id in ipairs(selected) do
        local n = Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len()
        if table.contains(data[2][2], id) then
          n1 = n1 + n
        else
          n2 = n2 + n
        end
      end
      return n1 >= n2 + Fk:translate(Fk:getCardById(to_select).trueName, "zh_CN"):len()
    end
  end,
  feasible = function(selected, data, extra_data)
    if #selected > 0 then
      local n1, n2 = 0, 0
      for _, id in ipairs(selected) do
        local n = Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len()
        if table.contains(data[2][2], id) then
          n1 = n1 + n
        else
          n2 = n2 + n
        end
      end
      return n1 >= n2 and n2 > 0
    end
  end,
}
bowen:addEffect(fk.AfterCardUseDeclared, {
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self, true) and player.phase == Player.Play and
      player:getMark("xiaobai__bowen-phase") < 5
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "xiaobai__bowen-phase", 1)
  end,
})

return bowen